package com.controller;

import com.pojo.book;
import com.service.bookService;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AddSuccessServlet extends HttpServlet {

    private Map<String, List<book>> cars=new LinkedHashMap<>();
    private bookService bookService=new bookService();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print("我要<a href=\"index.jsp\">继续购买</a><br/>");
        String bookid = req.getParameter("bookid");
        String count = req.getParameter("bookCount"); //购买count本书
        int bookcount = Integer.parseInt(count);
        HttpSession session = req.getSession();
        try {
            ResultSet resultSet = bookService.queryOne(bookid);
            String uid="";


            Map<String, List<book>> car = (Map<String, List<book>>) session.getAttribute("car");

            if(car==null||car.size()==0){
                List<book> books=new ArrayList<>();
              while (resultSet.next()){
                  if(bookcount>resultSet.getInt(4)){
                      writer.print("你购买书的数量大于现有的书的数量，请适当买少一点");
                  break;
                  }else{
                      books.add(new book(resultSet.getString(1),resultSet.getString(2),resultSet.getDouble(3),bookcount));
                  }
              }
                Cookie[] cookies = req.getCookies();
                for (int i = 0; i < cookies.length; i++) {
                    if(cookies[i].getName().equals("loginUser")){
                     uid=   cookies[i].getValue();
                        break;
                    }
                }
                 cars.put(uid,books);
                 session.setAttribute("car",cars);


            }else{
                Map<String, List<book>> car1 = (Map<String, List<book>>) session.getAttribute("car");
                Cookie[] cookies = req.getCookies();
                for (int i = 0; i < cookies.length; i++) {
                    if(cookies[i].getName().equals("loginUser")){
                        uid=   cookies[i].getValue();
                        break;
                    }
                }
                List<book> books = car1.get(uid);
                int index=-1;
                int bcount=-1;
                //通过index去判断想买的书是否已经在购物车中
                for (int i = 0; i < books.size(); i++) {
                    if(books.get(i).getBookid().equals(bookid)){
                        index=i;
                        bcount=books.get(i).getBookcount();
                        break;
                    }
                }


                while (resultSet.next()){
                    if(bookcount>resultSet.getInt(4)){
                        writer.print("你购买书的数量大于现有的书的数量，请适当买少一点");
                        break;
                    }else{
                        if(index==-1){
                            books.add(new book(resultSet.getString(1),resultSet.getString(2),resultSet.getDouble(3),bookcount));
                            break;
                        }else{
                            books.remove(index);
                            books.add(index,new book(resultSet.getString(1),resultSet.getString(2),resultSet.getDouble(3),bookcount+bcount));
                            break;
                        }

                    }
                }
                cars.put(uid,books);
                session.setAttribute("car",cars);

            }


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        writer.print("放入购物车成功<br/>");
        writer.print("我要<a href=\"showServlet\">查看购物车</a>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
